package ac.java.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {
	/**
	 * 根据开始时间和结束时间获得week段
	 * 
	 * @param begin
	 * @param end
	 * @param returnTypeSign
	 *            ,返回的list中的时间段是date还是second
	 */
	@SuppressWarnings("unchecked")
	public List findTimeSection(String beginTime, String endTime, String returnTypeSign, String unitSign) {
		List list = new ArrayList();
		List millisList = new ArrayList();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar beginC = Calendar.getInstance();
		Calendar endC = Calendar.getInstance();
		try {
			beginC.setTime(df.parse(beginTime));
			endC.setTime(df.parse(endTime));

		} catch (Exception e) {
			e.printStackTrace();
		}

		long day6millis = 6 * 24 * 60 * 60 * 1000;
		long day1millis = 1 * 24 * 60 * 60 * 1000 - 1;

		int dayUnit = 7;
		if (unitSign.equals("week")) {
			if ((endC.getTimeInMillis() - beginC.getTimeInMillis()) < (day6millis + day1millis)) {
				System.out.println("-在一周-");
				String[] s = { beginTime, endTime };
				long[] ls = { beginC.getTimeInMillis() / 1000, (endC.getTimeInMillis() + day1millis) / 1000 };
				list.add(s);
				millisList.add(ls);
				if (returnTypeSign.equals("date")) {
					return list;
				} else if (returnTypeSign.equals("second")) {
					return millisList;
				} else {
					return list;
				}
			}
		} else if (unitSign.equals("month")) {
			if ((beginC.get(Calendar.YEAR) == endC.get(Calendar.YEAR)) && (beginC.get(Calendar.MONTH) == endC.get(Calendar.MONTH))) {

				System.out.println("-在同一个月-");
				String[] s = { beginTime, endTime };
				long[] ls = { beginC.getTimeInMillis() / 1000, (endC.getTimeInMillis() + day1millis) / 1000 };
				list.add(s);
				millisList.add(ls);
				if (returnTypeSign.equals("date")) {
					return list;
				} else if (returnTypeSign.equals("second")) {
					return millisList;
				} else {
					return list;
				}
			}
		} else if (unitSign.equals("day")) {
			while (true) {
				String[] dayArrayMiddle = new String[2];
				long[] millisArrayMiddle = new long[2];
				dayArrayMiddle[0] = df.format(beginC.getTime());
				millisArrayMiddle[0] = beginC.getTimeInMillis() / 1000;
				if (beginC.getTimeInMillis() <= endC.getTimeInMillis()) {

					dayArrayMiddle[1] = df.format(beginC.getTime());
					millisArrayMiddle[1] = (beginC.getTimeInMillis() + day1millis) / 1000;
					list.add(dayArrayMiddle);
					millisList.add(millisArrayMiddle);
					beginC.add(Calendar.DAY_OF_MONTH, 1);
				} else {
					break;
				}
			}
			if (returnTypeSign.equals("date")) {
				return list;
			} else if (returnTypeSign.equals("second")) {
				return millisList;
			} else {
				return list;
			}

		}

		// System.out.println("beginWeek-------"+beginWeek);
		// 开始的一天 begin
		int beginDay = beginC.get(Calendar.DAY_OF_WEEK) - 1;
		boolean unitEndDayFlag = false;
		if (unitSign.equals("week")) {
			if (beginDay == 0) {// 0表示星期天，此判断表示到了一周的最后一天
				unitEndDayFlag = true;
			}
		} else if (unitSign.equals("month")) {
			beginDay = beginC.get(Calendar.DAY_OF_MONTH);
			if (beginDay == beginC.getActualMaximum(Calendar.DAY_OF_MONTH)) {// 此判断表示到了一个月的最后一天
				unitEndDayFlag = true;
			}

		}

		String[] dayArrayBegin = new String[2];
		long[] millisArrayBegin = new long[2];
		dayArrayBegin[0] = beginTime;
		millisArrayBegin[0] = beginC.getTimeInMillis() / 1000;
		if (unitEndDayFlag) {
			dayArrayBegin[1] = beginTime;
			millisArrayBegin[1] = (beginC.getTimeInMillis() + day1millis) / 1000;
		} else {
			if (unitSign.equals("month")) {
				dayUnit = beginC.getActualMaximum(Calendar.DAY_OF_MONTH);
			}
			beginC.add(Calendar.DAY_OF_MONTH, (dayUnit - beginDay));
			dayArrayBegin[1] = df.format(beginC.getTime());
			millisArrayBegin[1] = (beginC.getTimeInMillis() + day1millis) / 1000;
		}
		list.add(dayArrayBegin);
		millisList.add(millisArrayBegin);
		// 开始的一天 end

		// System.out.println(df.format(beginCLate.getTime())+"====");
		// 中间的一天begin

		beginC.add(Calendar.DAY_OF_MONTH, 1);

		while (true) {
			String[] dayArrayMiddle = new String[2];
			long[] millisArrayMiddle = new long[2];
			dayArrayMiddle[0] = df.format(beginC.getTime());
			millisArrayMiddle[0] = beginC.getTimeInMillis() / 1000;
			long dayCha = day6millis;
			if (unitSign.equals("month")) {
				dayUnit = beginC.getActualMaximum(Calendar.DAY_OF_MONTH);
				dayCha = (long) (dayUnit - 1) * 24 * 60 * 60 * 1000;
			}
			if ((beginC.getTimeInMillis() + dayCha) <= endC.getTimeInMillis()) {

				beginC.add(Calendar.DAY_OF_MONTH, dayUnit - 1);
				dayArrayMiddle[1] = df.format(beginC.getTime());
				millisArrayMiddle[1] = (beginC.getTimeInMillis() + day1millis) / 1000;
				list.add(dayArrayMiddle);
				millisList.add(millisArrayMiddle);
				beginC.add(Calendar.DAY_OF_MONTH, 1);
			} else {
				break;
			}
		}
		// 中间的天 end

		// 最后的一天begin

		int endDay = endC.get(Calendar.DAY_OF_WEEK) - 1;
		boolean unitEndDayFlag2 = false;
		if (unitSign.equals("week")) {
			if (endDay == 0) {
				unitEndDayFlag2 = true;
			}
		} else if (unitSign.equals("month")) {
			endDay = endC.get(Calendar.DAY_OF_MONTH);
			if (endDay == endC.getActualMaximum(Calendar.DAY_OF_MONTH)) {
				unitEndDayFlag2 = true;
			}

		}

		String[] dayArrayEnd = new String[2];
		long[] millisArrayEnd = new long[2];
		dayArrayEnd[1] = endTime;
		millisArrayEnd[1] = (endC.getTimeInMillis() + day1millis) / 1000;
		if (!unitEndDayFlag2) {
			endC.add(Calendar.DAY_OF_MONTH, -(endDay - 1));
			dayArrayEnd[0] = df.format(endC.getTime());
			millisArrayEnd[0] = endC.getTimeInMillis() / 1000;
			list.add(dayArrayEnd);
			millisList.add(millisArrayEnd);
		}
		// 最后的一天 end

		if (returnTypeSign.equals("date")) {
			return list;
		} else if (returnTypeSign.equals("second")) {
			return millisList;
		} else {
			return list;
		}
	}

	public String findDateByMillis(long millis) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(millis);
		return df.format(c.getTime());
	}

	public String findDateBySecond(long second) {

		return findDateByMillis(second * 1000);
	}

	/**
	 * 根据时间字符串获得秒second
	 * 
	 * @param dateStr
	 * @return
	 */
	public long findSecondByDate(String dateStr) {
		return findMillisByDate(dateStr) / 1000;
	}

	/**
	 * 根据时间字符串获得毫秒millissecond
	 * 
	 * @param dateStr
	 * @return
	 */
	public long findMillisByDate(String dateStr) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(df.parse(dateStr));

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return c.getTimeInMillis();
	}

	/**
	 * 
	 * @param date
	 * @param sign
	 *            :time表示日期需要精确到秒，date表示日期精确到天
	 * @return
	 */
	public String findByDate(Date date, String sign) {
		if (sign.equals("time")) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.format(date);
		} else {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			return df.format(date);
		}

	}

	public static String getNow() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(currentTime);
	}

	public static void main(String[] args) {
		TimeUtil tt = new TimeUtil();
		// System.out.println(tt.getWeekdayOfDateTime("2011-5-6"));
		// List list=tt.findWeekSection("2011-6-5","2011-6-26");
		// List list=tt.findWeekSection("2011-6-2","2011-6-26");
		// List list=tt.findWeekSection("2011-6-5","2011-6-30");

		// 周
		String returnTypeSign = "second";// "date";//
		String unitSign = "month";// "month";"day";
		List list = tt.findTimeSection("2009-04-20", "2011-04-23", returnTypeSign, unitSign);
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

		for (int i = 0; i < list.size(); i++) {
			if (returnTypeSign.equals("second")) {
				long[] s = (long[]) list.get(i);
				System.out.print(s[0] + " 至 " + s[1] + "     ");
				Calendar ee = Calendar.getInstance();
				Calendar ee1 = Calendar.getInstance();
				ee.setTimeInMillis(s[0] * 1000);
				ee1.setTimeInMillis(s[1] * 1000);
				System.out.println(df.format(ee.getTime()) + " 至 " + df.format(ee1.getTimeInMillis()));
			} else {
				String[] s = (String[]) list.get(i);
				System.out.println(s[0] + " 至 " + s[1]);
			}

		}

		Calendar ee = Calendar.getInstance();

		Date date = new Date();

		try {
			ee.setTime(df.parse("2011/06/05"));
			// System.out.println(df.format(ee.getTime()) );
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("===========" + df.format(new Date()));
	}
}
